<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>

          <a-col :xs="24" :sm="12">
            <a-form-item label="活动名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入活动名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择开始时间" v-decorator="['startTime', validatorRules.startTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择结束时间" v-decorator="['endTime', validatorRules.endTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="是否可评论" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="radio" v-decorator="['messageType', validatorRules.messageType]" :trigger-change="true" dictCode="yn" placeholder="请选择是否可评论"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="每人每日可投票数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['voteNum', validatorRules.voteNum]" placeholder="请输入每人每日可投票数" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="每人可投总票数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['voteCount', validatorRules.voteCount]" placeholder="请输入每人可投总票数" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="封面图" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple v-decorator="['img', validatorRules.img]"></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="活动规则" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-editor v-decorator="['rule',{trigger:'input'}]"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="活动是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="radio" v-decorator="['enableStatus', validatorRules.enableStatus]" :trigger-change="true" dictCode="yn" placeholder="请选择活动是否启用"/>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="投票选项" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="voteOptionTable.loading"
            :columns="voteOptionTable.columns"
            :dataSource="voteOptionTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
        
      </a-tabs>

    </a-spin>
  </j-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JEditor from '@/components/jeecg/JEditor'

  export default {
    name: 'VoteModal',
    mixins: [JEditableTableMixin],
    components: {
      JDate,
      JImageUpload,
      JDictSelectTag,
      JEditor,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
validatorRules: {
        name: {
        rules: [
            { required: true, message: '请输入活动名称!'},
        ]
        },
        startTime: {
        rules: [
            { required: true, message: '请输入开始时间!'},
        ]
        },
        endTime: {
        rules: [
            { required: true, message: '请输入结束时间!'},
        ]
        },
        messageType: {
        rules: [
            { required: true, message: '请输入是否可评论!'},
        ]
        },
        voteNum: {
        rules: [
            { required: true, message: '请输入每人每日可投票数!'},
        ]
        },
        voteCount: {
        rules: [
            { required: true, message: '请输入每人可投总票数!'},
        ]
        },
        img: {
        rules: [
            { required: true, message: '请输入封面图!'},
        ]
        },
        rule: {
        rules: [
            { required: true, message: '请输入活动规则!'},
        ]
        },
        enableStatus: {
        rules: [
            { required: true, message: '请输入活动是否启用!'},
        ]
        },
},
        refKeys: ['voteOption', ],
        tableKeys:['voteOption', ],
        activeKey: 'voteOption',
        // 投票选项
        voteOptionTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '投票选项',
              key: 'item',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '票数',
              key: 'count',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/vote/vote/add",
          edit: "/vote/vote/edit",
          voteOption: {
            list: '/vote/vote/queryVoteOptionByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'name','startTime','endTime','messageType','voteNum','voteCount','img','rule','enableStatus')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.voteOption.list, params, this.voteOptionTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          voteOptionList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'name','startTime','endTime','messageType','voteNum','voteCount','img','rule','enableStatus'))
     },

    }
  }
</script>

<style scoped>
</style>